﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.Filter;
using Customer.Bussiness_Service;
using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceCore;

namespace Customer.Areas.Customer.Controllers
{
    [ApiController, Area("Customer"), Route("/repair/[action]"), Customer_Admin]
    public class repairController : ControllerBase
    {
        private readonly Irepair_service _service;
        private readonly IToole_DI _tool;
        public repairController(Irepair_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;
        }

        /// <summary>
        /// 获取报修分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_type()
        {
            var result = _service.get_repair_type();
            return result;
        }
        /// <summary>
        /// 保存报修分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel save_type([FromBody]List<repair_type> list)
        {
            var result = _service.save_repair_type(list);
            _tool.di_es.Add_Log($"==保存报修分类==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(list)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 登记报修
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel repair_register(repair_register_params model)
        {
            var result = _service.repair_register(model);
            _tool.di_es.Add_Log($"==登记报修==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        ///  获取报修信息详情
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel repair_model(string repair_id)
        {
            var result = _service.repair_model(repair_id);
            return result;
        }
        /// <summary>
        /// 根据用户获取对应报修列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel get_repair_pagelist(Base_List_Params query)
        {
            var result = _service.get_repair_pagelist(query);
            return result;
        }
        /// <summary>
        /// 获取当前登录用户的报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel get_my_repair_pagelist(search_my_repair_list query)
        {
            var result = _service.get_my_repair_pagelist(query);
            return result;
        }
        /// <summary>
        /// 转技术售后
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel give_technology_repair(string repair_id, string technology_uid)
        {
            var result = _service.give_technology_repair(repair_id, technology_uid);
            _tool.di_es.Add_Log($"==转技术售后==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_id, technology_uid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 申请审批
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel application_approval(string repair_id, string approval_uid, string repair_approval_des, approval_model? approval_model, string express_model, decimal? freight, decimal? approval_price,string express_no)
        {
            var result = _service.application_approval(repair_id, approval_uid, repair_approval_des, approval_model, express_model, freight, approval_price, express_no);
            _tool.di_es.Add_Log($"==申请审批==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_id, approval_uid, repair_approval_des, approval_model, express_model, freight, approval_price, express_no })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 申请审批
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel post_application_approval(application_approval_Params Params)
        {
            var result = _service.application_approval(Params.repair_id, Params.approval_uid, Params.repair_approval_des, Params.approval_model, Params.express_model, Params.freight, Params.approval_price, Params.express_no, Params.transfer_screenshot);
            _tool.di_es.Add_Log($"==申请审批==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 上传审批相关图片
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel upload_approval_pic(string repair_id, string repair_content_pic, string repair_express_no)
        {
            var result = _service.upload_approval_pic(repair_id, repair_content_pic, repair_express_no);
            _tool.di_es.Add_Log($"==上传审批相关图片==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_id, repair_content_pic, repair_express_no })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 根据业务登记获取员工列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_customer_userlist(string lv, approval_model approval_model = approval_model.在保审批)
        {
            var result = _service.get_customer_userlist(lv, approval_model);
            return result;
        }
        /// <summary>
        /// 保存报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel save_repair(save_repair_params model)
        {
            var result = _service.save_repair(model);
            _tool.di_es.Add_Log($"==保存报修信息==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }

        /// <summary>
        /// 获取我审核的列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel get_my_approval_pagelist(search_my_approval_list model)
        {
            var result = _service.get_my_approval_pagelist(model);
            return result;
        }

        /// <summary>
        /// 获取我审核的列表 导出excel
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void down_my_approval_pagelist_for_excel([FromForm] search_my_approval_list query)
        {
            _service.down_my_approval_pagelist_for_excel(query);

        } 
        /// <summary>
        /// 处理审核
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel handle_apply(string repair_id, apply_state? state, string approval_repair_handle_result)
        {
            var result = _service.handle_apply(new string[] { repair_id }, state, approval_repair_handle_result);
            _tool.di_es.Add_Log($"==处理审核==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_id, state, approval_repair_handle_result })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 批量处理审批
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel batch_handle_apply(batch_handle_apply_params Params)
        {
            var result = _service.handle_apply(Params.repair_ids, Params.state, Params.approval_repair_handle_result);
            _tool.di_es.Add_Log($"==批量处理审核==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        } 
        /// <summary>
        /// 修改寄件状态
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel handle_apply_express_state(string repair_id, express_state? state)
        {
            var result = _service.handle_apply_express_state(repair_id, state);
            _tool.di_es.Add_Log($"==修改寄件状态==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_id, state })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        } 
        /// <summary>
        /// 获取报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel admin_get_repair_pagelist(search_my_repair_list query)
        {
            var result = _service.admin_get_repair_pagelist(query);
            return result;
        }
        /// <summary>
        /// 获取报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_repair_express(string repair_id)
        {
            var result = _service.get_repair_express(repair_id);
            return result;
        }

        /// <summary>
        /// 获取报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<customer_repairForexcelPage> get_repair_pagelist_for_excel(search_my_repair_list_for_excel query)
        {
            var result = _service.get_repair_pagelist_for_excel(query);
            return result;
        }

        /// <summary>
        /// 获取报修信息转excel
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void down_repair_pagelist_for_excel([FromForm] search_my_repair_list_for_excel query)
        {
            _service.down_repair_pagelist_for_excel(query);
            
        }

        /// <summary>
        ///  配件返回确认
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel reback_data_info(reback_data_info_params Params)
        {
            var result = _service.reback_data_info(Params);
            _tool.di_es.Add_Log($"==配件返回确认==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        } 
        /// <summary>
        /// 获取审批列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<get_approval_pagelist_for_Excel> get_approval_pagelist(search_my_approval_list Params)
        {
            var result = _service.get_approval_pagelist(Params); 
            return result;
        }

        /// <summary>
        /// 导出审批列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void down_approval_pagelist_for_excel([FromForm] search_my_approval_list query)
        {
            _service.down_approval_pagelist_for_excel(query);
        }
        /// <summary>
        /// 提交一次返件审批
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel submit_return_approval(submit_return_approval_params model)
        {
            var result = _service.submit_return_approval(model);
            _tool.di_es.Add_Log($"submit_return_approval==提交一次返件审批==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
        /// <summary>
        /// 提交一次返件审批
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel submit_return_approval_for_zhanghuan(submit_return_approval_params model)
        {
            var result = _service.submit_return_approval_for_zhanghuan(model);
            _tool.di_es.Add_Log($"submit_return_approval_for_zhanghuan==提交一次返件审批==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        }
         

        /// <summary>
        /// 获取返件数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_return_approval(string repair_id)
        {
            var result = _service.get_return_approval(repair_id);
            return result;
        }

        /// <summary>
        /// 确认返件审核
        /// </summary>
        /// <param name="repair_return_id"></param>
        /// <param name="state"></param>
        /// <param name="return_approval_des"></param>
        /// <param name="open_add"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel handle_apply_return_state(string repair_return_id, return_approval_state? state, string return_approval_des, bool? open_add = false)
        {
            var result = _service.handle_apply_return_state(repair_return_id, state, return_approval_des, open_add);
            _tool.di_es.Add_Log($"handle_apply_return_state==确认返件审核==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { repair_return_id, state, return_approval_des, open_add })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "repair");
            return result;
        } 
        /// <summary>
        /// 确认返件审核
        /// </summary>
        /// <param name="repair_return_id"></param>
        /// <param name="state"></param>
        /// <param name="return_approval_des"></param>
        /// <param name="open_add"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<return_approval_item> get_return_approval_list(search_return_approval_list query)
        {
            var result = _service.get_return_approval_list(query);
            return result;
        } 
        /// <summary>
        /// 确认返件审核
        /// </summary>
        /// <param name="repair_return_id"></param>
        /// <param name="state"></param>
        /// <param name="return_approval_des"></param>
        /// <param name="open_add"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel get_over_insurance_approval_pagelist(search_my_approval_list query)
        {
            var result = _service.get_over_insurance_approval_pagelist(query);
            return result;
        }
        
    }
}